package com.heima.scan.runner;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.wemedia.entity.WmSensitive;
import com.heima.scan.mapper.WmSensitiveMapper;
import com.heima.utils.common.SensitiveWordUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 一个类实现了ApplicationRunner接口, 实现其中的run方法之后
 * 当spring项目启动完成之后就会自动执行其中的run方法 , 一般用于一些数据的初始化操作
 */
@Component
@Slf4j
public class SensitiveApplicationRunner implements ApplicationRunner {

    @Resource
    private WmSensitiveMapper wmSensitiveMapper;
    @Override
    public void run(ApplicationArguments args) throws Exception {

        //1.从数据表中查询敏感词列表
        List<WmSensitive> sensitiveList = wmSensitiveMapper.selectList(Wrappers.emptyWrapper());
        //当前敏感词列表为空,直接返回
        if(CollectionUtils.isEmpty(sensitiveList)){
            return ;
        }

        List<String> world = sensitiveList.stream().map(s -> s.getSensitives()).collect(Collectors.toList());
        //2.初始化DFA数据结构
        SensitiveWordUtil.initMap(world);

    }
}
